package com.syezon.wifikey.news.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.AppUtils;
import com.bumptech.glide.Glide;
import com.hodanet.appadvplatclient.ReportAgent;
import com.hodanet.appadvplatclient.ad.AdInfo;
import com.syezon.wifikey.R;
import com.syezon.wifikey.ad.AdPositionEnum;
import com.syezon.wifikey.ad.AdUtils;
import com.syezon.wifikey.analysis.AnalysisHelper;
import com.syezon.wifikey.channel.ChannelConfig;
import com.syezon.wifikey.web.WebHelper;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2018/9/30.
 */

public class TopAdListAdapter extends RecyclerView.Adapter<TopAdListAdapter.ItemViewHolder> {

    private Context mContext;
    private List<AdInfo> mData = new ArrayList<>();

    public TopAdListAdapter(Context context) {
        this.mContext = context;
}

    public void setData(List<AdInfo> data) {
        if (data != null && data.size() > 0) {
            mData.clear();
            mData.addAll(data);
            notifyDataSetChanged();
        }
    }

    @NonNull
    @Override
    public ItemViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new ItemViewHolder(LayoutInflater.from(mContext).inflate(R.layout.item_ad_list, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ItemViewHolder holder, int position) {
        final AdInfo adInfo = mData.get(position);
        Glide.with(mContext).load(adInfo.getPic()).into(holder.ivIcon);
        holder.tvTitle.setText(adInfo.getName());
        if (!adInfo.isReportExtShow()) {
            ReportAgent.reportAdvExtShow(adInfo, ChannelConfig.getChannel(), AppUtils.getAppVersionCode());
            adInfo.setReportExtShow(true);
        }
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AdUtils.adClicked(mContext, adInfo, AdPositionEnum.NEWS_TOP_LIST);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public class ItemViewHolder extends RecyclerView.ViewHolder {

        @BindView(R.id.iv_ad)
        ImageView ivIcon;
        @BindView(R.id.tv_ad_title)
        TextView tvTitle;

        public ItemViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
